package logger2.filter.gui;

import java.util.HashMap;
import java.util.Map;

import logger2.environment.Environment;
import logger2.filter.Filter;
import logger2.filter.FilterVisitor;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

public class MasterFilterPanel extends AbstractFilterPanel {
	private static final long serialVersionUID = -6768357434963391462L;
	private Map<Class<? extends Filter>, AbstractFilterPanel> panels = new HashMap<>();
	private AbstractFilterPanel currentPanel = null;
	
	public MasterFilterPanel(Environment env) {
		super(env);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0};
		gridBagLayout.rowHeights = new int[]{0};
		gridBagLayout.columnWeights = new double[]{1.0};
		gridBagLayout.rowWeights = new double[]{1.0};
		setLayout(gridBagLayout);
	}

	@Override
	public void setModel(Filter filter) {
		if(currentPanel != null) {
			currentPanel.setModel(null);
		}
		if(filter != null) {
			if(!panels.containsKey(filter.getClass())) {
				System.out.println(filter.getClass().getName());
				currentPanel = filter.acceptVisitor(new FilterVisitor<AbstractFilterPanel>() {
					@Override
					public AbstractFilterPanel visitConfigurableFilter() {
						return new ConfigurableFilterPanel(logEnvironment);
					}

					@Override
					public AbstractFilterPanel visitCompoundFilter() {
						return new CompoundFilterPanel(logEnvironment);
					}
					
					@Override
					public AbstractFilterPanel visitAcceptAllFilter() {
						return new AcceptAllFilterPanel(logEnvironment);
					}
				});
				panels.put(filter.getClass(), currentPanel);
				add(currentPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				revalidate();
			} else {
				currentPanel = panels.get(filter.getClass());
			}
			currentPanel.setModel(filter);
			repaint();
		}
	}

	@Override
	public Filter getModel() {
		if(currentPanel == null) 
			return null;
		return currentPanel.getModel();
	}


}
